/**
 * Created by lenovo on 2017/11/25.
 */
define(['ngApp', 'ngHttp', 'jquery', 'ngConstant', 'ngAlert'], function (ngApp, ngHttp, JQ, ngConstant, ngAlert) {
    ngApp.directive('onFinishRenderFilters', function ($timeout) {
        return {
            restrict: 'A',
            link: function (scope, element, attr) {
                if (scope.$last === true) {
                    $timeout(function () {
                        scope.$emit('ngRepeatFinished');
                    });
                }
            }
        }
    });
    ngApp.controller("leftTab1", function ($scope, $http, $timeout, $rootScope, $state, $uibModal, commonFn, $cookies,$window) {

        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            $rootScope.leftTabHeight = $('#leftTab1').height();
            $('.tabBox').height($rootScope.leftTabHeight);
        });
        //上方条件筛选的接口
        $scope.searchMsg = {};
        $scope.searchMsg.size  = ngConstant.size;
        //条件筛选获取   省市区接口  step1  获取省份
        $http({
            url: ngConstant.interfaceHost + 'admin/system/getAllProvinces',
            method: 'GET',
            headers: {'Authorization':$cookies.get("token")}
        }).then(function (data) {
            $scope.provinces = data.data.data.body.data;
        });
        //获取市
        $scope.getCity = function () {
            $scope.cityId = null;
            $scope.areaId = null;
            $http({
                url: ngConstant.interfaceHost + 'admin/system/getCitysByProvinceid',
                method: 'GET',
                params: {
                    "provinceid":$scope.provinceId
                },
                headers: {'Authorization':$cookies.get("token")}
            }).then(function (data) {
                $scope.cities = data.data.data.body.data;
            });
        };
        //获取区
        $scope.getArea = function () {
            $http({
                url: ngConstant.interfaceHost + 'admin/system/getAreasByCityid',
                method: 'GET',
                params: {
                    "cityid":$scope.cityId
                },
                headers: {'Authorization':$cookies.get("token")}
            }).then(function (data) {
                $scope.areas = data.data.data.body.data;
            });
        };
        $scope.searcBtnClick = function(){
            $scope.searchMsg.provinceid = $scope.provinceId;
            $scope.searchMsg.cityid = $scope.cityId;
            $scope.searchMsg.areaid = $scope.areaId;
            $http({
                url: ngConstant.interfaceHost + 'manager/store/findSearch',
                method: 'GET',
                params:$scope.searchMsg,
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                // if (res.data.data.content == 0) {
                //     ngAlert.successMsg('暂无数据')
                // } else {
                //
                // }

                $scope.pageMsg = res.data.data;
                $scope.storeTable = res.data.data.content;

            });
        };



        //点击重置密码
        $scope.resetPass = function(){
            console.log(this.item);
            $http({
                url: ngConstant.interfaceHost + 'admin/resetPwd',
                method: 'POST',
                params:{"userId":this.item.userId},
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                ngAlert.successMsg(res.data.data);
            });
        };

        //查询数据的函数封装
        function getTableData(){
            //查询table中所有数据接口
            $http({
                url: ngConstant.interfaceHost + 'manager/store/findAllStore',
                method: 'GET',
                params:{
                    "sort":"createDate,desc",
                    "page":$scope.page,
                    "size":ngConstant.size
                },
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                //页数数据
                $scope.pageMsg = res.data.data;
                console.log(res.data.data.content);
                //表格数据
                $scope.storeTable = res.data.data.content;
                //全选
                $scope.checked = [];
                $scope.checkedMsg = [];
                $scope.selectAll = function () {
                    if($scope.select_all) {
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                        angular.forEach($scope.storeTable, function (i) {
                            i.checked = true;
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        })
                    }else {
                        angular.forEach($scope.storeTable, function (i) {
                            i.checked = false;
                            $scope.checked = [];
                            $scope.checkedMsg = [];
                        })
                    }
                };
                $scope.selectOne = function ($event) {
                    console.log($event);
                    angular.forEach($scope.storeTable, function (i) {
                        var index = $scope.checked.indexOf(i.id);
                        if(i.checked && index === -1) {
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        } else if (!i.checked && index !== -1){
                            $scope.checked.splice(index, 1);
                            $scope.checkedMsg.splice(index, 1);
                        }
                    });

                    if ($scope.storeTable.length === $scope.checked.length) {
                        $scope.select_all = true;
                    } else {
                        $scope.select_all = false;
                    }
                };
            });
        }


        // //定义初始页数并查询数据
        $scope.page = 0;
        $scope.page1 = $scope.page + 1;
        $http({
            url: ngConstant.interfaceHost + 'manager/store/findAllStore',
            method: 'GET',
            params:{
                "sort":"createDate,desc",
                "page":$scope.page,
                "size":ngConstant.size
            },
            headers: {'Authorization':$cookies.get("token")}
        }).then(function(res){
            //页数数据
            $scope.pageMsg = res.data.data;
            console.log(res.data.data.content);
            //表格数据
            $scope.storeTable = res.data.data.content;

            //全选
            //点击下一页上一页时   $scope.select_all初始化置空   在点击事件中写入
            $scope.checked = [];
            $scope.checkedMsg = [];
            $scope.selectAll = function () {
                if($scope.select_all) {
                    $scope.checked = [];
                    $scope.checkedMsg = [];
                    angular.forEach($scope.storeTable, function (i) {
                        i.checked = true;
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    })
                }else {
                    angular.forEach($scope.storeTable, function (i) {
                        i.checked = false;
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                    })
                }
            };
            $scope.selectOne = function ($event) {
                console.log($event);
                angular.forEach($scope.storeTable, function (i) {
                    var index = $scope.checked.indexOf(i.id);
                    if(i.checked && index === -1) {
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    } else if (!i.checked && index !== -1){
                        $scope.checked.splice(index, 1);
                        $scope.checkedMsg.splice(index, 1);
                    }
                });

                if ($scope.storeTable.length === $scope.checked.length) {
                    $scope.select_all = true;
                } else {
                    $scope.select_all = false;
                }
            };




            //下一页
            $scope.nextPageBtn = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg.totalPages - 1) {
                    return false;
                } else {
                    $scope.page++;
                    $scope.page1++;
                    getTableData();
                }

            };

            //上一页
            $scope.prevPageBtn = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                } else {
                    $scope.page--;
                    $scope.page1--;
                    getTableData();
                }
            };

            //转到第几页
            $scope.myKeyup13 = function(e){
                $scope.select_all = false;
                //IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
                var keycode = window.event?e.keyCode:e.which;
                if(keycode==13){
                    $scope.page = $scope.page1 - 1;
                    getTableData();
                }
            };

            //首页
            $scope.firstPageBtn = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                }
                $scope.page = 0;
                $scope.page1 = 1;
                getTableData();
            };

            //尾页
            $scope.lastPageBtn = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg.totalPages - 1){
                    return false;
                }
                $scope.page = $scope.pageMsg.totalPages - 1;
                $scope.page1 = $scope.pageMsg.totalPages;
                getTableData();
            };

            //删除商户接口
            $scope.deleteMerchantBtn = function(){
                if ($scope.checkedMsg.length == 0) {
                    ngAlert.errorMsg('请选择要删除的数据');
                    return false;
                }
                ngAlert.open('确认删除本条记录？');
                console.log($scope.checked);
                console.log(angular.toJson($scope.checked));
                console.log($scope.checkedMsg);
                $('.layui-layer-btn').click(function(){
                    $http({
                        url: ngConstant.interfaceHost + 'manager/store/deleteStore',
                        method: 'POST',
                        data:angular.toJson($scope.checkedMsg) ,
                        headers: {'Authorization':$cookies.get("token")}
                    }).then(function(res){
                        ngAlert.successMsg(res.data.data);
                        getTableData();
                    });
                });

            };

            //审核商户接口
            $scope.auditMerchantBtn = function(){
                console.log($scope.checkedMsg);
                $http({
                    url: ngConstant.interfaceHost + 'manager/store/auditStore',
                    method: 'POST',
                    data:angular.toJson($scope.checkedMsg) ,
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    console.log(res);
                    ngAlert.successMsg(res.data.data);
                    getTableData();
                });
            };

        });


        //新增商户接口
        $scope.layerOutShow = false;
        $scope.addMerchantBtn = function(){
            $scope.layerOutShow = true;
            $scope.provinceId = null;
            $scope.cityId = null;
            $scope.areaId = null;
            //addMerchantMsg  点击提交需要发送的数据
            $scope.addMerchantMsg = {};
            $scope.addMerchantMsg.roleNum = 2;
            //省市区接口  step1  获取省份
            $http({
                url: ngConstant.interfaceHost + 'admin/system/getAllProvinces',
                method: 'GET',
                headers: {'Authorization':$cookies.get("token")}
            }).then(function (data) {
                $scope.provinces = data.data.data.body.data;
            });

            //获取市
            $scope.getCity = function () {
                $http({
                    url: ngConstant.interfaceHost + 'admin/system/getCitysByProvinceid',
                    method: 'GET',
                    params: {
                        "provinceid":$scope.provinceId
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function (data) {
                    $scope.cities = data.data.data.body.data;
                });
            };

            //获取区
            $scope.getArea = function () {
                $http({
                    url: ngConstant.interfaceHost + 'admin/system/getAreasByCityid',
                    method: 'GET',
                    params: {
                        "cityid":$scope.cityId
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function (data) {
                    $scope.areas = data.data.data.body.data;
                });
            };

            //点击提交发送请求
            $scope.layerOutSub = function(){
                //点击提交时候需要给后台发送的数据  （ng-model动态绑定）
                $scope.addMerchantMsg.username = $scope.addMerchantMsg.userObject.phoneNum;
                $scope.addMerchantMsg.userObject.provinceid = $scope.provinceId;
                $scope.addMerchantMsg.userObject.cityid = $scope.cityId;
                $scope.addMerchantMsg.userObject.areaid = $scope.areaId;
                var proObj = document.getElementsByName("proName")[0];
                var proLabel= proObj .options[proObj .selectedIndex].text;
                $scope.addMerchantMsg.userObject.province = proLabel;
                var cityObj = document.getElementsByName("cityName")[0];
                var cityLabel= cityObj .options[cityObj .selectedIndex].text;
                $scope.addMerchantMsg.userObject.city = cityLabel;
                var areaObj = document.getElementsByName("areaName")[0];
                var areaLabel= areaObj .options[areaObj .selectedIndex].text;
                $scope.addMerchantMsg.userObject.area = areaLabel;

                $http({
                    url: ngConstant.interfaceHost + 'admin/signup/storeSignup',
                    method: 'POST',
                    data:$scope.addMerchantMsg,
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    ngAlert.successMsg(res.data.data);
                    $timeout(function(){
                        $scope.layerOutShow = false;
                    },2000);
                    $scope.provinceId = null;
                    $scope.cityId = null;
                    $scope.areaId = null;
                    getTableData();
                },function(res){
                    ngAlert.errorMsg(res.data.errMsg);
                });
            };

        };



        //点击叉号关闭层
        $scope.closeLay = function(){
            $scope.provinceId = null;
            $scope.cityId = null;
            $scope.areaId = null;
            $scope.layerOutShow = false;
        };


    })
});